﻿using CarPCBATest.Procedure;
using Common;
using ImageSourceModuleCs;
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using VM.Core;
using VMControls.WPF.Release;

namespace CarPCBATest.ViewModel
{
    public partial class HomeViewModel
    {
        private bool Dx11_Common(LedCurrentConfig config)
        {
            bool comRet = true;
            Thread.Sleep(500);
            // 1.4测试电流
            double current = 0;
            try
            {
                current = DM3058E.GetCurrent() * 1000;
            }
            catch
            {
                current = DM3058E.GetCurrent() * 1000;
            }

            Log = $"电流为{current}mA";
            if (current <= config.UpperLimitCurrent & current >= config.LowerLimitCurrent)
            {
                // ok
            }
            else
            {
                //ng
                Log = "电流不在OK范围";
                TestResultDatas[0].NgMsg.Add($"功能[{config.LedDesc}] 电流=[{current}] 不在OK范围");
                comRet = false;
            }
            bool timeOut = false;
            vmProcHaveExec = false;
            swGlobal.Restart();
            vmResult = null;
            Application.Current.Dispatcher.Invoke(() =>
            {
                VisionTool.SetProcVisionRet(config.ProcName, control);
            });
            // 2.视觉检测 sdk获取图像
            CameraTool.Open();
            var image = CameraTool.GetCurrentImage();
            //设置到图像源
            ImageSourceModuleTool imageSourceModuleTool = (ImageSourceModuleTool)VmSolution.Instance[$"{config.ProcName}.图像源1"];
            imageSourceModuleTool.SetImageData(image);
            Task.Run(() =>
            {
                Thread.Sleep(3000);
                timeOut = true;
            });

            VisionTool.RunProc(config.ProcName);
            while (!timeOut)
            {
                if (vmProcHaveExec)
                {
                    break;
                }
                Thread.Sleep(10);
            }
            if (timeOut)
            {
                // 超时
                Log = "视觉判定流程执行超时";
                TestResultDatas[0].NgMsg.Add($"功能[{config.LedDesc}] 视觉判定流程执行超时");
                comRet = false;
            }
            if (vmResult is null)
            {
                Log = "视觉判定测试结果为null";
            }
            Mylog.Info($"vmResult=[{vmResult}]");
            if (vmResult == "1")
            {
                // ok
                Log = $"{config.LedDesc}测试OK";
            }
            else
            {
                //ng
                Log = $"{config.LedDesc}测试NG";
                Log = "灯光视觉判定NG";
                TestResultDatas[0].NgMsg.Add($"功能[{config.LedDesc}] 灯光视觉判定NG");
                comRet = false;
            }
            return comRet;
        }

        Stopwatch swGlobal = new Stopwatch();

        // 刹车灯
        public bool DX11_BREAK(LedCurrentConfig config)
        {
            try
            {
                currentProcId = config.ProcID;
                Log = $"开始测试{config.LedDesc}功能";
                // 1.开灯
                PcbCANControl.BreakLampStatus = true;
                PcbCANControl.PosLampStatus = false;
                PcbCANControl.TurnLampStatus = TurnLampStatus.AllOff;
                Thread.Sleep(100);  //等待稳定
                var tmp = Dx11_Common(config);

                return tmp;
            }
            catch (Exception ex)
            {
                Log = ex.Message;
                Mylog.Error(ex, ex.Message);
                TestResultDatas[0].NgMsg.Add($"功能[{config.LedDesc}] {ex.Message}");
                return false;
            }
        }

        // 位置灯
        public bool DX11_POS(LedCurrentConfig config)
        {
            try
            {
                currentProcId = config.ProcID;
                Log = $"开始测试{config.LedDesc}功能";
                // 1.开灯
                PcbCANControl.BreakLampStatus = false;
                PcbCANControl.PosLampStatus = true;
                PcbCANControl.TurnLampStatus = TurnLampStatus.AllOff;
                Thread.Sleep(100);  //等待稳定
                var tmp = Dx11_Common(config);

                return tmp;
            }
            catch (Exception ex)
            {
                Log = ex.Message;
                Mylog.Error(ex, ex.Message);
                TestResultDatas[0].NgMsg.Add($"功能[{config.LedDesc}] {ex.Message}");
                return false;
            }
        }

        // 左转向灯
        public bool DX11_TURN_L(LedCurrentConfig config)
        {
            try
            {
                currentProcId = config.ProcID;
                Log = $"开始测试{config.LedDesc}功能";
                // 1.开灯
                PcbCANControl.BreakLampStatus = false;
                PcbCANControl.PosLampStatus = false;
                PcbCANControl.TurnLampStatus = TurnLampStatus.OnlyLeftOn;
                Thread.Sleep(100);  //等待稳定
                var tmp = Dx11_Common(config);

                return tmp;
            }
            catch (Exception ex)
            {
                Log = ex.Message;
                Mylog.Error(ex, ex.Message);
                TestResultDatas[0].NgMsg.Add($"功能[{config.LedDesc}] {ex.Message}");
                return false;
            }
        }

        // 右转向灯
        public bool DX11_TURN_R(LedCurrentConfig config)
        {
            try
            {
                currentProcId = config.ProcID;
                Log = $"开始测试{config.LedDesc}功能";
                // 1.开灯
                PcbCANControl.BreakLampStatus = false;
                PcbCANControl.PosLampStatus = false;
                PcbCANControl.TurnLampStatus = TurnLampStatus.OnlyRightOn;
                Thread.Sleep(100);  //等待稳定
                var tmp = Dx11_Common(config);

                return tmp;
            }
            catch (Exception ex)
            {
                Log = ex.Message;
                Mylog.Error(ex, ex.Message);
                TestResultDatas[0].NgMsg.Add($"功能[{config.LedDesc}] {ex.Message}");
                return false;
            }
        }
    }
}